﻿using MediatR;
using SystemHYS.API.Application.command.GualitySchemeCommmand;
using SystemHYS.Domian;
using SystemHYS.Domian.DTO;
using SystemHYS.Infrstructre.Interface;
namespace SystemHYS.API.Application.commandler.GualitySchemeCommandler
{
    public class GualitySchemeModelCommmandler:IRequestHandler<GualitySchemeModelCommmand,GualitySchemeDTO>
    {
        IGualitySchemeRepository Repository;
        IProjectSchemeRepository projectSchemeRepository;
        IGualityProjectRepository ProjectRepository;
        public GualitySchemeModelCommmandler(IGualitySchemeRepository Repository, IProjectSchemeRepository projectSchemeRepository, IGualityProjectRepository projectRepository)
        {
            this.Repository = Repository;
            this.projectSchemeRepository = projectSchemeRepository;
            this.ProjectRepository = projectRepository;
        }

        public async Task<GualitySchemeDTO> Handle(GualitySchemeModelCommmand request, CancellationToken cancellationToken)
        {
            //质检项目
            var plist = (from d in await ProjectRepository.GetAllAsnc() select d).ToList();
            //质检关联
            var blist = (from b in await projectSchemeRepository.GetAllAsnc() select b).ToList();
            var list = (from a in await Repository.GetAllAsnc() where a.gualitySchemeId==request.gualitySchemeId
                        select new GualitySchemeDTO
                        {
                            gualitySchemeId = a.gualitySchemeId,                  
                            gualitySchemeNumbering = a.gualitySchemeNumbering,
                            gualitySchemeName = a.gualitySchemeName,
                            gualitySchemeState = a.gualitySchemeState,
                            gualityGroupId = a.gualityGroupId,
                            state = a.state,
                            AddName = a.AddName,
                            AddTime = a.AddTime,
                            Plist = (from b in plist
                                     join c in blist on b.gualityProjectId equals c.gualityProjectId where c.gualitySchemeId==a.gualitySchemeId select b).ToList()
                        });
            return list.FirstOrDefault();

        }
    }
}
